package org.csu.pet_store_2.Controller;
import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Category;
import org.csu.pet_store_2.persistence.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import org.csu.pet_store_2.Domain.Product;
import org.csu.pet_store_2.persistence.ProductMapper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;


@RestController
@Transactional
public class CategoryController {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    /*
    获取所有类别
     */
    @GetMapping("/categories")
    public CommonResponse<List<Category>> getallcategories()
    {
        List<Category> categoryList=categoryMapper.selectList(null);
        return CommonResponse.creatForSuccess(categoryList);
    }

    @GetMapping("/category")
    public CommonResponse<List<Product>> categroyForm(@RequestParam(name="category") String category,
                                               Model model){
        System.out.println(category);
        Category category1 =categoryMapper.getCategoryByUserid(category);
        System.out.println(category1);
        List<Product> productlist =productMapper.getProductsListByCategory(category);
        return CommonResponse.creatForSuccess("成功",productlist);

    }
    @GetMapping("/products")
    public CommonResponse<List<Product>> products(){
        List<Product> productlist =productMapper.getProductsList();
        return CommonResponse.creatForSuccess("成功",productlist);
    }

}
